package com.qiguliuxing.dts.service.merchant;

import com.github.pagehelper.PageHelper;
import com.qiguliuxing.dts.db.bean.MerchantAccountTrace;
import com.qiguliuxing.dts.db.dao.DtsMerchantAccountTraceMapper;
import com.qiguliuxing.dts.db.dao.DtsMerchantWithdrawalAccountMapper;
import com.qiguliuxing.dts.db.dao.ex.MerchantAccountTraceMapper;
import com.qiguliuxing.dts.db.domain.*;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.slf4j.LoggerFactory.getLogger;

/**
 * 商户提现账号服务
 * @author qiguliuxing
 * @since 1.0.0
 */
@Service
public class WithdrawAccountService {
    private static final Logger logger = getLogger(WithdrawAccountService.class);

    @Resource
    private DtsMerchantWithdrawalAccountMapper dtsMerchantWithdrawalAccountMapper;

    @Resource
    private DtsMerchantAccountTraceMapper dtsMerchantAccountTraceMapper;

    @Resource
    private MerchantAccountTraceMapper merchantAccountTraceMapper;

    /**
     * 根据商户与类型获取账号
     * @param brandId
     * @param withdrawType
     * @return
     */
    public DtsMerchantWithdrawalAccount getWithdrawAccount(Integer brandId, Integer withdrawType) {
        DtsMerchantWithdrawalAccountExample example = new DtsMerchantWithdrawalAccountExample();
        example.or().andPayTypeEqualTo(withdrawType).andBrandIdEqualTo(brandId);

        return dtsMerchantWithdrawalAccountMapper.selectOneByExample(example);
    }

    /**
     * 根据商户获取账号
     * @param brandId
     * @param  withdrawType
     * @return
     */
    public List<DtsMerchantWithdrawalAccount> getWithdrawAccount(Integer brandId,Integer withdrawType, Integer page, Integer size, String sort, String order) {
        DtsMerchantWithdrawalAccountExample example = new DtsMerchantWithdrawalAccountExample();
        DtsMerchantWithdrawalAccountExample.Criteria criteria = example.createCriteria();
        if (brandId != null) {
            criteria.andBrandIdEqualTo(brandId);
        }
        if (withdrawType != null) {
            criteria.andPayTypeEqualTo(withdrawType);
        }

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        example.or().andBrandIdEqualTo(brandId);
        return dtsMerchantWithdrawalAccountMapper.selectByExample(example);
    }

    /**
     * 根据商户获取账号
     * @param brandId
     * @return
     */
    public List<DtsMerchantWithdrawalAccount> getWithdrawAccount(Integer brandId, Integer page, Integer size, String sort, String order) {
        DtsMerchantWithdrawalAccountExample example = new DtsMerchantWithdrawalAccountExample();
        DtsMerchantWithdrawalAccountExample.Criteria criteria = example.createCriteria();
        if (brandId != null) {
            criteria.andBrandIdEqualTo(brandId);
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        example.or().andBrandIdEqualTo(brandId);
        return dtsMerchantWithdrawalAccountMapper.selectByExample(example);
    }


    public List<DtsMerchantAccountTrace> querySelective(Integer brandId, Integer page, Integer size, String sort, String order ,String accountNo, List<Byte> statusArray, List<LocalDateTime> times) {
        DtsMerchantAccountTraceExample example = new DtsMerchantAccountTraceExample();
        DtsMerchantAccountTraceExample.Criteria criteria = example.createCriteria();

        if (brandId != null) {
            criteria.andBrandIdEqualTo(brandId);
        }
        if (accountNo != null && accountNo != "") {
            criteria.andAccountNoEqualTo(accountNo);
        }
        if (statusArray != null && statusArray.size() > 0) {
            criteria.andStatusIn(statusArray);
        }
        if (times != null && times.size() == 2) {
            criteria.andAddTimeBetween(times.get(0),times.get(1));
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        return dtsMerchantAccountTraceMapper.selectByExample(example);
    }

    public List<DtsMerchantAccountTrace> querySelective(List<Integer> brandIds, Integer page, Integer size, String sort, String order,Integer brandId,String accountNo,List<Byte> statusArray) {
        DtsMerchantAccountTraceExample example = new DtsMerchantAccountTraceExample();
        DtsMerchantAccountTraceExample.Criteria criteria = example.createCriteria();
        if (brandId != null) {
            criteria.andBrandIdEqualTo(brandId);
        }
        if (accountNo != null && accountNo != "") {
            criteria.andAccountNoEqualTo(accountNo);
        }
        if (statusArray != null && statusArray.size() > 0) {
            criteria.andStatusIn(statusArray);
        }
        if (brandIds != null && brandIds.size() > 0) {
            criteria.andBrandIdIn(brandIds);
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, size);
        return dtsMerchantAccountTraceMapper.selectByExample(example);
    }

    public List<MerchantAccountTrace> queryMerchantAccountTrace(List<Integer> brandIds, Integer page, Integer size, String sort, String order,Integer brandId,String accountNo,List<Byte> statusArray) {
        Map<String,Object> param = new HashMap<String,Object>();
        if (brandId != null) {
            param.put("brandId",brandId);
        }
        if (accountNo != null && accountNo != "") {
            param.put("accountNo",accountNo);
        }
        if (statusArray != null && statusArray.size() > 0) {
            param.put("statusArray",statusArray);
        }
        if (brandIds != null && brandIds.size() > 0) {
            param.put("brandIds",brandIds);
        }
        PageHelper.startPage(page, size);
        return merchantAccountTraceMapper.selectByExample(param);
    }

    public DtsMerchantAccountTrace querySelective(Integer id) {
        return dtsMerchantAccountTraceMapper.selectByPrimaryKey(id);
    }

    public int updateByPrimaryKey(DtsMerchantAccountTrace record) {
        return dtsMerchantAccountTraceMapper.updateByPrimaryKey(record);
    }

    /**
     * 根据商户与类型获取账号
     * @param brandId
     * @param account
     * @return
     */
    public DtsMerchantWithdrawalAccount getWithdrawAccount(Integer brandId, String account) {
        DtsMerchantWithdrawalAccountExample example = new DtsMerchantWithdrawalAccountExample();
        example.or().andAccountEqualTo(account).andBrandIdEqualTo(brandId);
        return dtsMerchantWithdrawalAccountMapper.selectOneByExample(example);
    }
}
